package com.study.example.sync;

import org.slf4j.Logger;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import static org.slf4j.LoggerFactory.getLogger;

/**
 * @author 贺瑞杰
 * @version V1.0
 * @date 2018-06-17 0:03
 * @description
 */
public class SynchronizedExample1 {

	private static final Logger log = getLogger(SynchronizedExample1.class);

	/**
	 * synchronized 修饰一个代码块
	 * 作用于当前对象的
 	 */
	public void test1(){
		synchronized (this){
			for (int i = 0; i < 10; i++) {
				log.info("test1 - {}", i);
			}
		}
	}

	/**
	 * synchronized 修饰一个方法
	 * 作用于当前对象的
	 */
	public synchronized void test2(){
		for (int i = 0; i < 10; i++) {
			log.info("test2 - {}", i);
		}
	}

	public static void main(String[] args){
	    SynchronizedExample1 example = new SynchronizedExample1();
	    SynchronizedExample1 example1 = new SynchronizedExample1();
		ExecutorService service = Executors.newCachedThreadPool();
		service.execute(() -> {
			example.test1();
		});
		service.execute(() -> {
			example1.test1();
		});
	}




}
